package cn.jason.util;

import java.io.*;


/**
 * 文件操作.
 * author: cnJason
 */
public class FileUtils {

    /**
     * 删除目录
     *
     * @param dir
     */
    public static void deleteDir(File dir) {
        File[] filelist = dir.listFiles();
        for (File file : filelist) {
            if (file.isFile()) {
                file.delete();
            } else {
                deleteDir(file);
            }
        }
        dir.delete();
    }

    /**
     * 复制文件
     *
     * @param origin  原来的文件.
     * @param newFile 新的文件.
     * @throws IOException
     */
    public static void copy(File origin, File newFile)throws IOException{
        //如果目录不存在，则创建目录.
        if (!newFile.getParentFile().exists()) {
            newFile.getParentFile().mkdirs();
        }
        FileInputStream fis = null;
        FileOutputStream fos = null;

        fis = new FileInputStream(origin);
        fos = new FileOutputStream(newFile);
        byte[] buf = new byte[2048];
        int read;
        while ((read = fis.read(buf)) != -1) {
            fos.write(buf, 0, read);
        }
        fis.close();
        fos.close();
    }

    /**
     * 写文件.
     * @param filename 文件名
     * @param contentStr 内容
     * @param charset 编码.
     * @throws IOException
     */
    public static void writeFile(String filename, String contentStr, String charset) throws IOException {
        byte[] content = contentStr.getBytes(charset);
        FileOutputStream fos = new FileOutputStream(filename);
        fos.write(content);
        fos.close();
    }

    public static void writeFile(File file, String contentStr, String charset) throws  IOException {
        byte[] content = contentStr.getBytes(charset);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(content);
        fos.close();
    }

    public static void writeFileWithParent(String filename, String contentStr, String charset) throws  IOException {
        File file = new File(filename);
        File parent = file.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        byte[] content = contentStr.getBytes(charset);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(content);
        fos.close();
    }

    public static void writeFileWithParent(File file, String contentStr, String charset) throws IOException {
        File parent = file.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        byte[] content = contentStr.getBytes(charset);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(content);
        fos.close();
    }

    public static void writeFile(String filename, byte[] content) throws  IOException {
        FileOutputStream fos = new FileOutputStream(filename);
        fos.write(content);
        fos.close();
    }


    public static void writeFile(File file, byte[] content) throws  IOException {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(content);
        fos.close();
    }

    public static void writeFileWithParent(String filename, byte[] content) throws  IOException {
        File file = new File(filename);
        File parent = file.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(content);
        fos.close();
    }

    public static void writeFileWithParent(File file, byte[] content) throws  IOException {

        File parent = file.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(content);
        fos.close();
    }

    public static byte[] readFile(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        byte[] buf = new byte[2048];
        int read;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((read = fis.read(buf)) != -1) {
            bos.write(buf, 0, read);
        }

        fis.close();
        return bos.toByteArray();
    }

}
